package com.sjlh.centurylianhua.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sjlh.centurylianhua.dto.SearchCon;
import com.sjlh.centurylianhua.entity.Role;
import com.sjlh.centurylianhua.mapper.RoleMapper;
import com.sjlh.centurylianhua.utils.ResultData;
import com.sjlh.centurylianhua.utils.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/role")
@CrossOrigin
public class RoleController {
    @Resource
    private RoleMapper roleMapper;
    /**
     * 分页查询
     *
     * */
    @RequestMapping("/getAll")
    public Object getAll(@RequestBody SearchCon searchCon){
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isnotNull(searchCon.getName()), "RoleName", searchCon.getName());
        // 封装分页对象
        Page<Role> page = new Page<Role>(searchCon.getPage(), searchCon.getLimit());
        Page<Role> rolePage = roleMapper.selectPage(page, wrapper);
        List<Role> list=rolePage.getRecords();//所有数据
        long count=rolePage.getTotal();//总条数
        ResultData rs=new ResultData(true,20000,"成功",list,count);
        return rs;
    }
    /**
     *根据角色名字精确查询角色
     * */
    public Role getOne(@RequestBody Role role){
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.eq( "RoleName", role.getRoleName());
        return roleMapper.selectOne(wrapper);
    }

    /**
     *
     * */
    @RequestMapping("/add")
    public Object add(@RequestBody Role role){
        Role o = this.getOne(role);
        if (o == null) {
            int r=roleMapper.insert(role);
            if(r==1)
                return new ResultData(true,20000,"添加角色成功",true,null);
            else
                return new ResultData(false,20000,"添加角色失败",false,null);
        } else {
            return new ResultData(false,20001,"角色名已存在",false,null);
        }

    }
    /**
     *
     * */
    @RequestMapping("/set")
    public Object set(@RequestBody Role role){
        Role o = this.getOne(role);
        if (o == null) {
            int r = roleMapper.updateById(role);
            if(r==1)
                return new ResultData(true,20000,"修改角色成功",true,null);
            else
                return new ResultData(false,20000,"修改角色失败",false,null);
        } else {
            return new ResultData(false,20001,"修改过的角色名已存在",null,null);
        }

    }
    /**
     *
     * */
    @RequestMapping("/del")
    public Object del(@RequestBody Role role){
        int rs = roleMapper.deleteById(role.getRoleId());

        if(rs==1)
            return new ResultData(true,20000,"删除成功",true,null);
        else
            return new ResultData(false,20001,"删除失败",false,null);
    }
}
